package cn.chenxiejia;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/***
 * @program: maven
 * @ClassName: Imges3
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/21 22:52
 * @version : V1.0
 */
public class Imges3 {
    public static void main(String[] args) throws IOException {
        BufferedImage i = new BufferedImage(800, 800, 1);
        var m = i.createGraphics();
        //设置抗锯齿（平滑度）
        m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        m.setColor(Color.YELLOW);
        m.fillRect(0, 0, 800, 800);
        m.setColor(Color.BLACK);
        //虚线
        for (int j = 0; j < 100; j++) {
            int x = j * 10 + 5;
            int x1 = j * 10 + 10;
            int y = 400;
            m.drawLine(x, y, x1, y);
        }
        for (int j = 0; j < 100; j++) {
            int x = j * 10 + 5;
            int x1 = j * 10 + 10;
            int y = 400;
            m.drawLine(y, x, y, x1);
        }
        m.setColor(Color.red);
        //文字设置
        Font font = new Font("微软雅黑", Font.BOLD, 30);
        m.setFont(font);
        String text = "此版权-陈卸甲";
        var fm = m.getFontMetrics(font);
        var tw = fm.stringWidth(text);
        var th = fm.getHeight();
        //Ascent是baseline之上至字符最高处的距离,Descent是baseline之下至字符最低处的距离
        m.drawString(text, i.getWidth() / 2 - tw / 2, i.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2);
        m.drawString(text, i.getWidth() - tw - 10, i.getHeight() - fm.getDescent() - 10);
        m.drawString(text, 10, fm.getAscent() + 10);
        m.dispose();
        ImageIO.write(i, "jpg", new File("strimg.jpg"));
        Runtime.getRuntime().exec("cmd /k start strimg.jpg");

    }
}
